from awesum import Drawable, Vector3, Sphere
from random import random
from block import Block

class Ball(Drawable):
	BOUNCE_VEL = .25
	DROP_ACC = -.01
	
	def __init__(self, v):
		super(Ball, self).__init__(Drawable.Traits.GL | Drawable.Traits.MOVE | Drawable.Traits.COLL)
		
		self.origin = v
	
	def load(self, game):
		super(Ball, self).load(game)
		self.move.velocity.x = random() *.1 + .02
		self.move.velocity.y = Ball.BOUNCE_VEL
		self.move.acceleration.y = Ball.DROP_ACC
		#Build a sphere! Bounding box info is automatically generated
		self.gl.createSphere(Sphere.Paint.COLOR, [Vector3(random() + .2, random() + .2, random() + .2)])
		#Scale down the sphere and bounding box
		self.gl.scaleV(Vector3(.5, .5, .5))
	
	def step(self):
		super(Ball, self).step()
		
		if self.origin.y <= 0:
			self.move.velocity.y = Ball.BOUNCE_VEL
		if self.origin.x <= 0 or self.origin.x >= 9.9:
			self.move.velocity.x = -self.move.velocity.x
	
	def collide(self, drawable, side):
		#If it collided on the sides
		if side == Drawable.CollisionFlags.X_POS and self.move.velocity.x > 0:
			self.move.velocity.x = -self.move.velocity.x
		elif side == Drawable.CollisionFlags.X_NEG and self.move.velocity.x < 0:
			self.move.velocity.x = -self.move.velocity.x
		#If it collided with a block above,
		elif side == Drawable.CollisionFlags.Y_POS:
			#Reverse Y velocity to go downward
			if self.move.velocity.y > 0:
				self.move.velocity.y = -self.move.velocity.y
		#If it collided with a block below,
		elif side == Drawable.CollisionFlags.Y_NEG:
			#Check what type of block and add appropriate upward velocity
			if isinstance(drawable, Block):
				if drawable.type == Block.Type.BOOST:
					self.move.velocity.y = Ball.BOUNCE_VEL * 1.25
				elif drawable.type == Block.Type.REDUCE:
					self.move.velocity.y = -self.move.velocity.y * .7
				else:
					self.move.velocity.y = -self.move.velocity.y
			else:
				self.move.velocity.y = Ball.BOUNCE_VEL / 2